<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected function _initDoctype()
    {
		$this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_TRANSITIONAL');
    }

    protected function _initConfig()
    {
        //载入系统配置信息
        $this->bootstrap('cache');
        $cache = $this->getResource('cache');
        $cacheId = 'config';
        if (!($config = $cache->load($cacheId))) {
            $this->bootstrap('db');
            $configTable = new Model_DbTable_Config();
            $data = $configTable->fetchAll('1')->toArray();
            foreach ($data as $value){
                $config[$value['key']] = $value['value'];
            }
            $cache->save($config);
        }
        if (!($cache->test($cacheId))) {
            $this->bootstrap('db');
            $configTable = new Model_DbTable_Config();
            $data = $configTable->fetchAll('1')->toArray();
            foreach ($data as $value){
                $config[$value['key']] = $value['value'];
            }
            $cache->save($config);
        } else {
            $config = $cache->load($cacheId);
        }
        //配置信息传递给view
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->config = $config;
        Zend_Registry::getInstance()->set('config',$config);
        return $config;
    }

    protected function _initCache()
    {
        $frontendOptions = array(
    		'caching' => true ,  //是否开启缓存
    		'cache_id_prefix' => 'Hm_' ,  //缓存前缀
    		'lifeTime' => 7200 ,  // 两小时的缓存生命期
    		'automatic_serialization' => true
        );
        $backendOptions = array(
    		'cache_dir' => APPLICATION_PATH . '/cache' // 放缓存文件的目录
        );
        $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
        Zend_Registry::getInstance()->set('cache',$cache);
        return $cache;
    }

    protected function _initLogger()
    {
        $logger = new Zend_Log();
        $this->bootstrap('db');
        $db = $this->getResource('db');
        $columnMapping = array('priority' => 'priority', 'message' => 'message', 'timestamp' => 'timestamp');
        $dbWritter = new Zend_Log_Writer_Db($db,'logs',$columnMapping);
        $logger->addWriter($dbWritter);
        if($this->_application->getEnvironment() != 'production'){
            $fbWritter = new Zend_Log_Writer_Firebug();
            $logger->addWriter($fbWritter);
        }
        return $logger;
    }

    protected function _initAuther()
    {
        $this->bootstrap('config');
        $config = $this->getResource('config');
        $auth = Zend_Auth::getInstance()->setStorage(new Hm_Auth_Storage_Cookie('Hm_user', $config['cookie_encode']));
        return $auth;
    }

    protected function _initAcl()
    {
        $this->bootstrap('cache');
        $cache = $this->getResource('cache');
        $acl = Model_Acl::getInstance($cache);
        return $acl;
    }

}